package soccer.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import soccer.model.goals.Goal;
import soccer.model.matches.Match;
import soccer.model.matches.MatchConstant;
import soccer.model.matches.MatchDAOImpl;
import soccer.model.matchesDetail.MatchesDetail;
import soccer.model.matchesDetail.MatchesDetailDAOImpl;
import soccer.model.player.Player;
import soccer.model.team.Team;
import soccer.model.team.TeamDAOImpl;

public class MatchActions {
   
	public List<Match> searchMatchByTeam(String teamName){

	List<Match> matches = null ;
    MatchDAOImpl matchDaoImpl = new MatchDAOImpl();
    List teamMatches = matchDaoImpl.searchMatchByTeam(teamName);
      if(teamMatches == null || teamMatches.size() < 1){
    	  return matches;
      }
      matches = new ArrayList<Match>();
      TeamDAOImpl teamDAOImpl = new TeamDAOImpl();
      MatchesDetailDAOImpl matchesDetailDAOImpl = new MatchesDetailDAOImpl();
      for(int i = 0; i < teamMatches.size(); i++){
    	  Map match = (Map) teamMatches.get(i);
    	  Map teamMap = (Map) teamDAOImpl.getTeams((Integer)match.get("homeTeamId")).get(0);
    	  Team homeTeam = new Team((Integer)teamMap.get("teamKodu"),(String)teamMap.get("teamName"),(String)teamMap.get("establishDate").toString(),(String)teamMap.get("city"));
          Map team = teamDAOImpl.getTeams((Integer)match.get("awayTeamId")).get(0);
          Team awayTeam = new  Team((Integer)match.get("awayTeamId"),(String)team.get("teamName") ,team.get("establishDate").toString(),(String) team.get("city"));
          Match newMatch = new Match(awayTeam, homeTeam, match.get("matchDate").toString());
          newMatch.setMatchId((Integer)match.get("matchId"));
          if((Integer)match.get("played") == 1){
        	  Map matchDetailMap = (Map) matchesDetailDAOImpl.getMatchDetailOfMatch(newMatch.getMatchId()).get(0);
        	  MatchesDetail matchDetail = new MatchesDetail((Integer)matchDetailMap.get("homeTeamSkor"),(Integer) matchDetailMap.get("awayTeamSkor"));
        	  matchDetail.setMatchDetailId((Integer)matchDetailMap.get("matchDetailId"));
        	  newMatch.setMatchDetail(matchDetail);
          }
          matches.add(newMatch);
      }
	   return matches;
   }
    public List<Match> searchMatchByDate(String matchDate){
    	List<Match> matches = null;
    	MatchDAOImpl matchDAOImpl = new MatchDAOImpl();
    	List matchList  = matchDAOImpl.searchMatchByDate(matchDate);
    	
    	if(matchList == null || matchList.size() < 1){
    		return matches;
    	}
    	matches = new ArrayList<Match>();
    	TeamDAOImpl teamDAOImpl = new TeamDAOImpl();
        MatchesDetailDAOImpl matchesDetailDAOImpl = new MatchesDetailDAOImpl();
    	for(int i = 0; i < matches.size(); i++){
    	  Map matchMap = (Map) matchList.get(i);	
    	  Map teamMap = (Map) teamDAOImpl.getTeams((Integer)matchMap.get("homeTeamId")).get(0);
    	  Team homeTeam = new Team((Integer)teamMap.get("teamKodu"),(String)teamMap.get("teamName"),(String)teamMap.get("establishDate"),(String)teamMap.get("city"));
          teamMap = (Map)teamDAOImpl.getTeams((Integer)matchMap.get("awayTeamId")).get(0);
          Team awayTeam = new Team((Integer)teamMap.get("teamKodu"),(String)teamMap.get("teamName"),(String)teamMap.get("establishDate"),(String)teamMap.get("city"));
          Match newMatch = new Match(awayTeam, homeTeam, matchDate);
          newMatch.setMatchId((Integer)matchMap.get("matchId"));
          if((Integer)matchMap.get("played") == 1){
        	  Map matchDetailMap = (Map) matchesDetailDAOImpl.getMatchDetailOfMatch(newMatch.getMatchId()).get(0);
        	  MatchesDetail matchDetail = new MatchesDetail((Integer)matchDetailMap.get("homeTeamSkor"),(Integer) matchDetailMap.get("awayTeamSkor"));
        	  newMatch.setMatchDetail(matchDetail);
            }
          matches.add(newMatch);
        }
      return matches;
   }
    
    public List<Player> getHomePlayerOfMatch(MatchesDetail md){
    	  MatchesDetailDAOImpl matchesDetailDAOImpl = new MatchesDetailDAOImpl();
    	  List homePlayersMapList = matchesDetailDAOImpl.getHomePlayerOfMatchDetail(md);
    	  List<Player> players = new  ArrayList<Player>();
    	  for(int i = 0; i < homePlayersMapList.size(); i++){
    		  Map playerMap = (Map) homePlayersMapList.get(i);
    		  String name = playerMap.get("name").toString();
    		  String surname = playerMap.get("surname").toString();
    		  String birthDate = playerMap.get("birthDate").toString();
    		  String birtPlace = playerMap.get("birthPlace").toString();
    		  int licenceNo = (Integer)playerMap.get("licenceNo");
    		  Player newPlayer = new Player(name,surname,birthDate,birtPlace,licenceNo);
    		  players.add(newPlayer);
    	  }
    	  return players;
    }
   public List<Player> getAwayPlayerOfMatch(MatchesDetail md){
	  MatchesDetailDAOImpl matchesDetailDAOImpl = new MatchesDetailDAOImpl();
	  List awayPlayersMapList = matchesDetailDAOImpl.getAwayPlayerOfMatchDetail(md);
	  List<Player> players = new  ArrayList<Player>();
	  for(int i = 0; i < awayPlayersMapList.size(); i++){
		  Map playerMap = (Map) awayPlayersMapList.get(i);
		  String name = playerMap.get("name").toString();
		  String surname = playerMap.get("surname").toString();
		  String birthDate = playerMap.get("birthDate").toString();
		  String birtPlace = playerMap.get("birthPlace").toString();
		  int licenceNo = (Integer)playerMap.get("licenceNo");
		  Player newPlayer = new Player(name,surname,birthDate,birtPlace,licenceNo);
		  players.add(newPlayer);
	 }
	  return players;
  }
  
  public List getHomeGoalsPlayersOfMatch(MatchesDetail md){
	   MatchesDetailDAOImpl matchesDetailDAOImpl = new MatchesDetailDAOImpl();
	   List scorerPlayersMapList =  matchesDetailDAOImpl.getHomeGoalsOfMatchDetail(md);
	   return scorerPlayersMapList;
  }
  
  public List getAwayGoalsPlayersOfMatch(MatchesDetail md){
	   MatchesDetailDAOImpl matchesDetailDAOImpl = new MatchesDetailDAOImpl();
	   List scorerPlayersMapList =  matchesDetailDAOImpl.getAwayGoalsOfMatchDetail(md);
	   return scorerPlayersMapList;
 }
  
  public List getMatchOfTeam(String teamName){
	  ArrayList matches = new ArrayList();
	  MatchDAOImpl matchDAOImpl = new MatchDAOImpl();
	  List matchList = matchDAOImpl.getMatchOfTeam(teamName);
	  List <Match> drawMatches = new ArrayList();
	  List <Match> wonMatches = new ArrayList();
	  List <Match> lostmatches = new ArrayList();
	  for(int i = 0; i < matchList.size(); i++){
		  Map matchMap = (Map) matchList.get(i);
		  Team home = new Team();
		  Team away = new Team();
		  Match match = null;
		  MatchesDetail matchesDetail = null;
		  int homeSkor = (Integer)matchMap.get("homeTeamSkor");
		  int awaySkor = (Integer)matchMap.get("awayTeamSkor");
		  matchesDetail = new MatchesDetail(homeSkor, awaySkor);
		  if(teamName.equals(matchMap.get("homeTeam").toString())){
			home.setTeamName(teamName);
			away.setTeamName(matchMap.get("awayTeam").toString());
			match = new Match();
           	if(homeSkor > awaySkor){
           		wonMatches.add(match);
           	}else if(homeSkor < awaySkor){
           		lostmatches.add(match);
           	}else{
           		drawMatches.add(match);
           	}
           	
		  }else if(teamName.equals(matchMap.get("awayTeam").toString())){
			  away.setTeamName(teamName);
			  home.setTeamName(matchMap.get("homeTeam").toString());
			  match = new Match();
				if(homeSkor < awaySkor){
	           		wonMatches.add(match);
	           	}else if(homeSkor > awaySkor){
	           		lostmatches.add(match);
	           	}else{
	           		drawMatches.add(match);
	           	}
		  }
		    match.setMatchDate(matchMap.get("matchDate").toString());
		    match.setHomeTeam(home);
			match.setAwayTeam(away);
			match.setMatchDetail(matchesDetail);
	  }
	  matches.add(MatchConstant.LOST_MATCHES, lostmatches);
	  matches.add(MatchConstant.DRAW_MATCH,drawMatches);
	  matches.add(MatchConstant.WON_MATCHES,wonMatches);
	  return matches;
  }
  public List getWonMatchOfTeam(String teamName){
	return null;   
  }
  public List getDrawMatchofTeam(String teamName){
     return null;
  }
}
